package com.logic.landseaserver.common.util;

import com.logic.system.service.IConfigurationService;
import com.logic.system.service.impl.ConfigurationService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.logic.common.util.SysUtil;
import com.logic.system.config.MybatisConfiguration;

import net.sf.json.JSONObject;

@Service
public class SMSUtil implements EnvironmentAware {

    private static RelaxedPropertyResolver propertyResolver;
    private static final String ENV_SMS_INFO = "logic.sms.";

    private static final String GET_TOKEN_URL = "tokenUrl";
    private static final String USER_NAME = "userName";
    private static final String PASS_WORD = "password";
    private static final String SEND_MSG_URL = "msgUrl";
    private static final Logger logger = LoggerFactory.getLogger(MybatisConfiguration.class);

    /**
     * @param messageCode
     * @param cellPhone
     * @param params      need to be assigned
     * @return
     */
    public static boolean sendSms(String messageCode, String cellPhone, String params,String signName) {
        if (StringUtils.isEmpty(cellPhone)) {
            logger.error("send msg is error cellphone and messageCode is null");
            return false;
        }
        // 获取 发送短信信息
        String tokenUrl = "", userName = "", password = "", msgUrl = "";
        tokenUrl = propertyResolver.getProperty(GET_TOKEN_URL, String.class, null);
        userName = propertyResolver.getProperty(USER_NAME, String.class, null);
        password = propertyResolver.getProperty(PASS_WORD, String.class, null);
        msgUrl = propertyResolver.getProperty(SEND_MSG_URL, String.class, null);
        if (StringUtils.isEmpty(tokenUrl) || StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(msgUrl) || StringUtils.isEmpty(messageCode)) {
            logger.error("send msg is error,get send msg env info is null");
            return false;
        }
        String token = getToken(tokenUrl, userName, password);
        if (StringUtils.isEmpty(token)) {
            logger.error("send msg is error,got token is error");
            return false;
        }

        String sign = SysUtil.MD5Digest(cellPhone + token + messageCode);
        JSONObject smsJson = new JSONObject();
        smsJson.put("phone", cellPhone);
        smsJson.put("message_code", messageCode);
        smsJson.put("sign_name", signName);
        smsJson.put("param", params);
        smsJson.put("sign", sign);
        String result = HttpRequestClient.postRequest(msgUrl, smsJson.toString(), token);
        logger.info("sendSms->result=" + result);
        JSONObject tokenJson = JSONObject.fromObject(result);
        //logger.info("send sms return :"+tokenJson.toString());
        String status = tokenJson.getString("STATUS");
        if (status.equals("0")) {
            return true;
        } else {
            return false;
        }
    }

    public static String getToken(String tokenUrl, String userName, String password) {
        String token = "";
        JSONObject gTokenJson = new JSONObject();
        gTokenJson.put("userName", userName);
        gTokenJson.put("password", password);

        String result = HttpRequestClient.postRequest(tokenUrl, gTokenJson.toString(), null);
        JSONObject tokenJson = JSONObject.fromObject(result);
        token = tokenJson.getString("token");
        return token;
    }

    @Override
    public void setEnvironment(Environment environment) {
        propertyResolver = new RelaxedPropertyResolver(environment, ENV_SMS_INFO);
    }

}
